package com.novax.ex.activity.provider.service;

import cn.hutool.core.collection.CollUtil;
import com.novax.ex.activity.infrastructure.entity.TaskEtRewards;
import com.novax.ex.activity.infrastructure.mapper.TaskEtRewardsMapper;
import com.novax.ex.activity.open.model.query.TaskEtRewardsQuery;
import com.novax.ex.activity.open.model.response.task.TaskEtRewardsResponse;
import com.novax.ex.common.base.BaseMapper;
import com.novax.ex.common.base.BaseService;
import com.novax.ex.common.util.CopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service
@Slf4j
public class TaskEtRewardsService extends BaseService {

    @Resource
    private TaskEtRewardsMapper taskEtRewardsMapper;

    @Override
    protected BaseMapper getBaseMapper() {
        return taskEtRewardsMapper;
    }

    /**
     * 根据用户ID查询用户获得Et情况
     *
     * @param uid 用户ID
     * @return 用户获取Et奖励的情况
     */
    public List<TaskEtRewards> findEtRewardByUid(Long uid) {
        return taskEtRewardsMapper.selectEtAwardByUid(uid);
    }

    /**
     * @Description: 更新活动解冻余额
     * @Author: ChenXi
     * @Date:  2023/6/28 10:15
     * @param userId
     * @param amount
     * @return: void
     **/
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean addUnfreeze(Long userId, BigDecimal amount, String currency) {
        return taskEtRewardsMapper.updateUnfreeze(userId, amount, currency) > 0;
    }

    /**
     * @Description: 查询资产时查询活动冻结资产
     * @Author: ChenXi
     * @Date:  2023/6/28 15:32
     * @param userId
     * @return: java.util.Map<java.lang.String,java.math.BigDecimal>
     **/
    public Map<String, BigDecimal> findFrozen(Long userId) {
        Map<String, BigDecimal> map = new HashMap<>();
        try {
            // 如果有多条脏数据 也使其正常返回  spot和wallet查资产时都会调用此接口
            List<TaskEtRewards> taskEtRewards = taskEtRewardsMapper.findAllByUserId(userId);
            if(CollUtil.isNotEmpty(taskEtRewards)){
                taskEtRewards.forEach(e -> map.put(e.getCurrency(),e.getAwaitUnfreeze()));
            }
        } catch (Exception e) {
            log.error("TaskEtRewardsService findFrozen error userId:{}", userId);
        }
        return map;
    }

    /**
     * @Description: 根据用户id查询
     * @Author: ChenXi
     * @Date:  2023/6/28 16:25
     * @param userId
     * @return: com.novax.ex.activity.infrastructure.entity.TaskEtRewards
     **/
    public TaskEtRewards findByUserId(Long userId, String currency){
        return taskEtRewardsMapper.findByUserId(userId, currency);
    }


    public TaskEtRewardsResponse total(TaskEtRewardsQuery query) {
        TaskEtRewards taskEtRewards = taskEtRewardsMapper.total(Collections.singletonMap("query", query));
        if (Objects.isNull(taskEtRewards)) {
            return new TaskEtRewardsResponse(BigDecimal.ZERO, BigDecimal.ZERO);
        }
        return CopyUtils.copyObject(taskEtRewards, TaskEtRewardsResponse.class);
    }
}
